<template>
    <div>
        <el-row :gutter="4">
            <el-col :span="4">
                <el-autocomplete
                        class="inline-input"
                        prefix-icon="fa fa-search"
                        v-model="keyword.teacherName"
                        :fetch-suggestions="querySearch"
                        placeholder="教师姓名"
                        :trigger-on-focus="false"
                        @keyup.enter.native="initResearchPaper"
                        v-permission="'research:paper:list'"
                        clearable
                ></el-autocomplete>
            </el-col>
            <el-col :span="4">
                <el-input v-model.trim="keyword.researchCourseBookName" prefix-icon="fa fa-search" placeholder="教材名称"
                          v-permission="'research:paper:list'"
                          @keyup.enter.native="initResearchPaper"></el-input>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" icon="fa fa-search" @click="initResearchPaper"
                           v-permission="'research:paper:list'"> 搜 索
                </el-button>
            </el-col>
            <el-col :span="2" :offset="2">
                <el-button type="primary" icon="fa fa-angle-double-down" @click="initResearchPaper"
                           v-permission="'research:paper:list'"> 高 级 搜 索
                </el-button>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" icon="fa fa-plus" @click="handleAdd"
                           v-permission="'research:paper:add'"> 添 加 信 息
                </el-button>
            </el-col>
            <el-col :span="2">
                <el-button type="danger" icon="fa fa-trash" @click="handleMultipleDelete"
                           v-permission="'research:paper:delete'"> 删
                    除 多 个
                </el-button>
            </el-col>
            <el-col :span="1" style="margin-left: 3px;">
                <el-button type="warning" icon="fa fa-refresh"
                           v-permission="'research:paper:list'"
                           @click="refresh">
                </el-button>
            </el-col>
        </el-row>
        <!--        表单 -->
        <el-row>
            <el-col :span="24">
                <el-table width="100%" border :data="page.row" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="50" align="center"></el-table-column>

                    <el-table-column prop="teacherName" label="教师姓名" align="center" width="120" fixed></el-table-column>
                    <!--      详情信息         -->
                    <el-table-column label="详情" align="center" width="80">
                        <template slot-scope="scope">
                            <el-button @click="viewDetails(scope.row)" type="text">查看详情</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column prop="researchPaperDescription" label="刊物备注" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperFirstAuthor" label="刊物第一作者" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperImportantReprint" label="刊物重要转载情况（例如EI/SCI）" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperName" label="刊物题目" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperNormalReprint" label="刊物普通转载" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperOtherAuthor" label="刊物其他作者" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperPublishDate" label="刊物出版时间" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperPublisherCn" label="刊物CN号" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperPublisherLevel" label="刊物等级" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperPublisherName" label="刊物名称" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperScore" label="刊物折合科研分" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperSubject" label="刊物所属学科" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperTopicNum" label="刊物成果所属课题编号" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperVolumesNum" label="刊物期（卷）数" align="center" width="120"></el-table-column>
                    <el-table-column prop="researchPaperYear" label="刊物研究年度" align="center" width="120"></el-table-column>
                    <el-table-column prop="teacherCollegeId" label="教工号" align="center" width="120"></el-table-column>
                    <el-table-column prop="teacherDegree" label="学位" align="center" width="120"></el-table-column>
                    <el-table-column prop="teacherDepartment" label="部门" align="center" width="120"></el-table-column>
                    <el-table-column prop="teacherGender" label="性别" align="center" width="120"></el-table-column>
                    <el-table-column prop="teacherId" label="教师外键" align="center" width="120"></el-table-column>
                    <el-table-column prop="teacherName" label="教师姓名" align="center" width="120"></el-table-column>
                    <el-table-column prop="teacherRank" label="职称" align="center" width="120"></el-table-column>

                    <el-table-column fixed="right" label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button @click="handleEdit(scope.row)" icon="fa fa-pencil"
                                       v-permission="'research:paper:update'"> 修 改
                            </el-button>
                            <el-button type="danger" @click="handleDelete(scope.row)" icon="fa fa-times-rectangle"
                                       v-permission="'research:paper:delete'"> 删 除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <!--                侧边弹出框 -->
        <el-drawer
                :title="data.teacherName"
                :visible.sync="drawer"
                direction="rtl"
                :before-close="handleDrawerClose"
                append-to-body
                close-on-press-escape
        >
            <span>{{data.teacherName}}</span>
        </el-drawer>
        <!--        分页 -->
        <el-row v-if="page.total > 10">
            <el-col :span="22">
                <el-pagination v-if="page.total > 10"
                               @size-change="handleSizeChange"
                               @current-change="handleCurrentChange"
                               :current-page="pagination.current"
                               :page-sizes="[9 ,10, 20, 30, 50]"
                               :page-size="pagination.size"
                               layout="total, sizes, prev, pager, next"
                               :total="page.total">
                </el-pagination>
            </el-col>
        </el-row>
        <!--    添加修改    -->
        <el-dialog
                v-dialog-drag
                :title="isAdd ? '新 增 数 据' : '修 改 数 据'"
                :visible.sync="centerDialogVisible"
                width="60%"
                center
                @close="resetField"
        >
            <el-form ref="form" :rules="rules" :model="data" label-width="80px">
                <el-form-item label="教师名:" label-width="80px" prop="teacherName">
                    <el-autocomplete
                            class="inline-input"
                            prefix-icon="fa fa-search"
                            v-model="data.teacherName"
                            :fetch-suggestions="querySearch"
                            placeholder="教师名"
                            :trigger-on-focus="false"
                            @select="handleSelect"
                            clearable
                    ></el-autocomplete>
                </el-form-item>
                <el-form-item label="教工号:" label-width="80px" prop="teacherCollegeId">
                    <el-input v-model="data.teacherCollegeId" placeholder="教工号" disabled></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOrUpdateResearchPaper">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {form, recommendTeacher} from "../../../api/modules/dictionary";
    import {
        page,
        addResearchPaper,
        deleteMultipleResearchPaper,
        deleteResearchPaper,
        updateResearchPaper
    } from "../../../api/modules/research/paper";

    export default {
        name: 'ResearchPaper',

        data() {
            return {
                keyword: {},
                data: {},
                pagination: {
                    current: 1,
                    size: 9
                },
                page: {
                    row: [],
                    total: 0
                },
                multipleSelection: [],
                centerDialogVisible: false,
                // 推荐信息
                recommend: {
                    teacher: [],
                    form: {}
                },
                isAdd: true,
                drawer: false,
                rules: {
                    teacherName: [{required: true, message: '请输入教师名', trigger: 'blur'}]
                }
            };
        },
        methods: {
            initRecommend() {
                recommendTeacher().then(response => {
                    this.recommend.teacher = response;
                });
                form().then(response => {
                    this.recommend.form = response;
                });
            },
            initResearchPaper() {
                page(this.pagination, this.keyword).then(response => {
                    this.page.row = response.row;
                    this.page.total = response.total;
                })
            },
            // 查询
            querySearch(queryString, cb) {
                cb(this.recommend.teacher.filter(item => item.value.indexOf(queryString) !== -1))
            },
            // 搜索
            handleSelect(data) {
                this.data.teacherCollegeId = data.jobNumber;
                this.data.teacherName = data.value;
                this.data.id = data.id;
            },
            // 当前页大小改变
            handleSizeChange(size) {
                this.pagination.size = size;
                this.initResearchPaper();
            },
            // 当前页改变
            handleCurrentChange(current) {
                this.pagination.current = current;
                this.initResearchPaper();
            },
            //添加数据或者修改数据
            addOrUpdateResearchPaper() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        // 是新增就进行添加
                        if (this.isAdd) {
                            addResearchPaper(this.data).then(() => {
                                // TODO 添加信息 不需要 init
                                this.centerDialogVisible = false;
                                this.initResearchPaper();
                            })
                        } else {
                            updateResearchPaper(this.data).then(() => {
                                // TODO 修改信息 不需要 init
                                this.centerDialogVisible = false;
                                this.initResearchPaper();
                            })
                        }
                    }
                })
            },
            // 修改单个
            handleEdit(row) {
                // 不是添加
                this.isAdd = false;
                Object.assign(this.data, row);
                this.centerDialogVisible = true;
            },
            handleAdd() {
                // 是添加
                this.isAdd = true;
                Object.assign(this.data, {});
                this.centerDialogVisible = true;
            },
            // 删除单个
            handleDelete(row) {
                this.$confirm(`此操作将永久删除 <strong><i> ${row.researchIntellectualName} </i></strong> 是否继续?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    dangerouslyUseHTMLString: true,
                    type: 'warning'
                }).then(() => {
                    deleteResearchPaper(row.id).then(() => {
                        // 刷新页面
                        this.initResearchPaper();
                    });
                })

            },
            // 多选框
            handleSelectionChange(row) {
                // 获取 ids
                this.multipleSelection = row.map(item => item.id);
            },
            // 删除多个
            handleMultipleDelete() {
                // 大于 0 才删除
                if (this.multipleSelection.length > 0) {
                    this.$confirm(`此操作将永久删除<strong> 多条信息 </strong>是否继续?`, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        dangerouslyUseHTMLString: true,
                        type: 'warning'
                    }).then(() => {
                        deleteMultipleResearchPaper(this.multipleSelection).then(() => {
                            // 刷新页面
                            this.initResearchPaper();
                        });
                    })
                }
            },
            viewDetails(row) {
                this.drawer = true;//打开滑块，并且将数据传输到data里面去
                Object.assign(this.data, row);
            },
            resetField() {
                // 清空表单验证
                this.$refs.form.resetFields();
            },
            refresh() {
                this.initResearchPaper();
                this.initRecommend();
            },
            handleDrawerClose() {
                this.drawer = false
                this.resetField();
            }
        },

        mounted() {
            this.refresh();
        },

        computed: {},

        watch: {}
    }
</script>

<style scoped>

</style>